package g4meTwo;

import java.awt.Dimension;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;


public class MobileItem extends Image{

	private String name;
	private float xl;
	private float yl;
	private float x;
	private float y;
	private float x1;
	private float x2;
	private float y1;
	private float y2;
	private boolean isEmpty;
	Dimension tailleEcran = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
	int hauteur = (int)tailleEcran.getHeight();
	int largeur = (int)tailleEcran.getWidth();

	public MobileItem(String name,boolean isEmpty, float x, float y, float xl, float yl, String data)
			throws SlickException {
		super(data);
		this.name = name;
		this.isEmpty = isEmpty;
		this.x = x;
		this.xl = xl;
		this.y = y;
		this.yl = yl;
		x1 = x - xl;
		x2 = x + xl;
		y1 = y - yl;
		y2 = y + yl;

	}
public void autoMove(int i, float hip, GameContainer gc){
	if (i == 0 || i==8) {
		borderX(gc);
		borderY(gc);
		this.setY(this.getY() - 3 * hip);
		this.update();
	}
	if (i == 1 || i==9) {
		borderX(gc);
		borderY(gc);
		this.setY(this.getY() + 3 * hip);
		this.update();
	}
	if (i == 2) {
		borderY(gc);
		borderX(gc);
		this.setX(this.getX() - 3 * hip);
		this.update();
	}
	if (i == 3) {
		borderY(gc);
		borderX(gc);
		this.setX(this.getX() + 3 * hip);
		this.update();
	}
	if (i == 4) {
		borderX(gc);
		borderY(gc);
		this.setY(this.getY() - 3 * hip);
		this.setX(this.getX() - 3 * hip);
		this.update();
	}
	if (i == 5) {
		borderX(gc);
		borderY(gc);
		this.setY(this.getY() - 3 * hip);
		this.setX(this.getX() + 3 * hip);
		this.update();
	}
	if (i == 6) {
		borderX(gc);
		borderY(gc);
		this.setY(this.getY() + 3 * hip);
		this.setX(this.getX() + 3 * hip);
		this.update();
	}
	if (i == 7) {
		borderX(gc);
		borderY(gc);
		this.setY(this.getY() + 3 * hip);
		this.setX(this.getX() - 3 * hip);
		this.update();
	}
	
}
public void borderY(GameContainer gc) {
	float y = this.getY();
	if (y < 0) {
		this.setY(hauteur);
	}
	if (y > hauteur) {
		this.setY(0);
	}

}

public void borderX(GameContainer gc) {
	float x = this.getX();

	if (x > largeur) {
		this.setX(0);
	}
	if (x < 0) {
		this.setX(largeur);
	}
}

	public float getX1() {
		return x1;
	}

	public float getXl() {
		return xl;
	}

	public void setXl(float xl) {
		this.xl = xl;
	}

	public float getYl() {
		return yl;
	}

	public void setYl(float yl) {
		this.yl = yl;
	}

	public void setX1(float x1) {
		this.x1 = x1;
	}

	public float getX2() {
		return x2;
	}

	public void setX2(float x2) {
		this.x2 = x2;
	}

	public float getY1() {
		return y1;
	}

	public void setY1(float y1) {
		this.y1 = y1;
	}

	public float getY2() {
		return y2;
	}

	public void setY2(float y2) {
		this.y2 = y2;
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
	}
	public void setData(String data){
		super.ref = data;		
	}
	public boolean getIsEmpty(){
		return isEmpty;
	}
	public void update(){
		x1 = x - xl;
		x2 = x + xl;
		y1 = y - yl;
		y2 = y + yl;
	}
	public boolean near(MobileItem item2,int range) {
		if(item2.getIsEmpty() == true){
			return false;
		}
		for (int i = (int) this.getX1() - range ; i < this.getX2() + range; i++) {
			for (int j = (int) this.getY1() - range; j < this.getY2() + range; j++) {
				if (i < item2.getX2() + range  && i > item2.getX1() - range && j < item2.getY2() + range
						&& j > item2.getY1() - range) {
					return true;
				}
			}
		}
		return false;
	}
	public String toString(){
		return "item : "+name+"\nx = "+x+"; y = "+y+"\nx1 = "+x1+"; y1 = "+y1+"\nx2 = "+x2+"; y2 = "+y2;
	}
}
